/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pagenumbergenerator;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.util.Random;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;

/**
 *
 * @author nika
 */

public class Pagenumbergenerator implements ActionListener{

    JFrame jf;
    JPanel jp;
    JRadioButton jrb1, jrb2, jrb3, jrb4, jrb5;
    ButtonGroup bg;
    JTextArea jta;
    JScrollPane jsp;
    JButton jb1,jb2, jb3 ;
    JCheckBox jcb1, jcb2;
    JTextField jtf1, jtf2;
    boolean b1,b2,b3, text1,text2;
    JLabel jl1;
    int i,j;

    
    Pagenumbergenerator(){
          
        jl1= new JLabel("      Range                                                 ->");
        jl1.setBounds(25, 150, 500, 30);
        jta= new JTextArea("");
        jta.setEditable(true);
        jta.setLineWrap(true);
        jta.setWrapStyleWord(true);
        //jta.setBounds(0, 0, 500, 200);
        jsp= new JScrollPane(jta, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jsp.setBounds(35, 250, 450, 200);
        //jsp.setLayout(null);
        jb1 = new JButton("Print");
        jb1.setBounds(350, 200, 100, 30);
        jb1.addActionListener(this);

        jb2 = new JButton("clear");
        jb2.setBounds(35, 200, 100, 30);
        jb2.addActionListener(this);

        jb3 = new JButton("About");
        jb3.setBounds(225, 475, 100, 25);
        jb3.addActionListener(this);

        
        jcb1= new JCheckBox("Append Output");
        jcb1.setSelected(false);
        jcb1.setBounds(175, 200, 150, 30);
        
        jcb2= new JCheckBox("In Reverse Order");
        jcb2.setSelected(false);
        jcb2.setBounds(275, 50, 150, 30);
        
        
        jtf1= new JTextField("Starting page No.");
        jtf1.setBounds(115, 150, 160, 30);
        //jtf1.
        jtf2= new JTextField("Ending Page No.");
        jtf2.setBounds(315, 150, 160, 30);
        
        
        try{
            //jtf11.addKeyListener(this);
       jtf1.addFocusListener(new FocusListener() {       
                

           @Override
           public void focusGained(FocusEvent e) {
               System.out.println("Focus1 Gained");
               String st="Starting page No.";
               System.out.println("Focus2 Gained");
               if(jtf1.getText().equals(st))
                       {jtf1.setText("");
                       }               
              // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
           }

           @Override
           public void focusLost(FocusEvent e) {
               System.out.println("Focus2 lost");
           String st2="";    
           System.out.println("Focus2 lost");
               if(jtf1.getText().equals(st2))
               {
                   jtf1.setText("Starting page No.");
               }
           
               
               //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
           }
           
          
           
       }             );
        
       
       jtf2.addFocusListener(new FocusListener() {       
          public void focusGained(FocusEvent e) {
               String st="Ending Page No.";
               System.out.println("Focus2 Gained");
               if(jtf2.getText().equals(st))
                       {jtf2.setText("");
                       }
           }

           
           public void focusLost(FocusEvent e) {
           
           String st2="";    
           System.out.println("Focus2 lost");
               if(jtf2.getText().equals(st2))
               {
                   jtf2.setText("Ending Page No.");
               }
           }
           
       }             );
        
        }
        catch (Exception errp) 
        {
            System.out.println(""+errp);
        }

        
        
        
        jrb1= new JRadioButton("All");
        jrb1.setBounds(35, 15, 200, 30);
        jrb1.setSelected(true);
        
        jrb2= new JRadioButton("Odd");
        jrb2.setBounds(35, 45, 200, 30);
        
        jrb3= new JRadioButton("Even");
        jrb3.setBounds(35, 75, 200, 30);
        
        jrb4= new JRadioButton("Random");
        jrb4.setBounds(35, 105, 200, 30);
        
        jrb5= new JRadioButton("Prime");
        jrb5.setBounds(275, 15, 200, 30);
        
        bg= new ButtonGroup();
        bg.add(jrb1);
        bg.add(jrb2);
        bg.add(jrb3);
        bg.add(jrb4);
        bg.add(jrb5);
        
        jp= new JPanel();
        jp.setLayout(null);
        jp.setSize(600, 600);
        jp.setLocation(25, 25);
        jp.add(jrb1);
        jp.add(jrb2);
        jp.add(jrb3);
        jp.add(jrb4);
        jp.add(jrb5);
        jp.add(jcb1);
        jp.add(jcb2);
        jp.add(jl1);
        jp.add(jb1);
        jp.add(jb2);
        jp.add(jsp);
        jp.add(jtf1);
        jp.add(jtf2);
        jp.add(jb3);
        
        
        
        jf= new JFrame("Page Number Generator");
        jf.setSize(575, 600);
        jf.setLayout(null);
        jf.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width-jf.getWidth())/2-100,
                            (Toolkit.getDefaultToolkit().getScreenSize().height-jf.getWidth())/2-100);
        jf.add(jp);
        jf.setDefaultCloseOperation(jf.EXIT_ON_CLOSE);
        jf.setVisible(true);
        jf.setIconImage(Toolkit.getDefaultToolkit().getImage("/home/nika/NetBeansProjects/Page Number Generator/icons/01.png"));
        
    
    }
    
    public void itemStateChanged(ItemEvent e) {
        Object source = e.getItemSelectable();
    
    
    }
    
    public void actionPerformed(ActionEvent ae) {
        Object o = ae.getSource();
String st="Starting page No.";
String st3="Ending Page No.";
        if(o==jb1){
    if ((jtf1.getText().equals(st))||(jtf2.getText().equals(st3))){

    javax.swing.JOptionPane.showMessageDialog(null, "Please select range correctly. Enter page number as such Starting Page Number must be smaller\n than Ending Page NUmber.", "Not Valid Range !", 0);
}
    else{
        print();
    }
    }
    
    if (o==jb2){
    jta.setText("");
    
    }
    
    if(o==jb3){
    about();
    }
    
    
    }

    public void about(){
    
    JFrame about= new JFrame("About");
    JPanel abtpanel= new JPanel();
    JPanel imgpanel= new JPanel();
    abtpanel.setLayout(null);
    abtpanel.setSize(650, 900);
    JLabel as= new JLabel("<HTML><font face=Georgia, Arial, Garamond><PRE><b>About This Software !</b> </PRE><BR><PRE>This software is for generating various numbers which can be </PRE><BR><PRE>used in printing wizard. This software is made as a solution </PRE><BR><PRE>for generating different types of numbers within a specific </PRE><BR><PRE>range. This tool is developed in Java by </PRE><BR><PRE>                       Navdeep Singh Sidhu             </PRE><BR><PRE>under practicing & testing his programming skill as a hobby ! </PRE><BR><PRE>Thank you for using this tool !!     </PRE><BR><PRE>    <b>                     MADE IN INDIA  </b>  </PRE></HTML>");
    as.setBounds(30, 10, 600, 450);
    JLabel am= new JLabel("<HTML><font face=Georgia, Arial, Garamond><PRE><b>About Developer !</b> </PRE><BR><PRE>Navdeep Singh Sidhu        </PRE><BR><PRE>Qualification : M.tech (C.S.E.), B.tech(C.S.E.) from GNDU(India)  </PRE><BR><PRE>Progamming for fun !      </PRE><BR><PRE>       </PRE></HTML>");
    am.setBounds(30, 450, 600, 250);
    //ImageIcon pic= new ImageIcon("/home/nika/NetBeansProjects/Page Number Generator/icons/02.JPG").getImage();
    //imgpanel.setBounds(30, 400, 250, 250);
    //imgpanel.add(pic);
    abtpanel.add(am);
    abtpanel.add(as);
        
        about.setSize(650, 750);
        about.setLayout(null);
        about.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width-about.getWidth())/2-100,
                            (Toolkit.getDefaultToolkit().getScreenSize().height-about.getWidth())/2-100);
        about.add(abtpanel);
        about.setDefaultCloseOperation(about.EXIT_ON_CLOSE);
        about.setVisible(true);
    
    
    
    }

    
public void print(){
 i= Integer.parseInt(jtf1.getText());
 j= Integer.parseInt(jtf2.getText());

{

if(i<=j)
{if(jcb1.isSelected()==false){
jta.setText("");
}

    if(jrb1.isSelected()){
        if(jcb2.isSelected()){
            printallr();
        }else{
        printall();
        }
    }

    if(jrb2.isSelected()){
    if(jcb2.isSelected()){
        printoddr();        
    }
    else{
        printodd();        
    
    }}
    
    
    if(jrb3.isSelected()){
    if(jcb2.isSelected()){
        printevenr();
    }else{
        printeven();
    
    }
    }
    
    if(jrb4.isSelected()){
    if(jcb2.isSelected()){
        printrandomr();
    }else{
        printrandom();
    
    }
    }
    
    if (jrb5.isSelected()){
    if(jcb2.isSelected()){
        printprimer();
    }else{
        printprime();
    
    }
    }
    
}
else{
javax.swing.JOptionPane.showMessageDialog(null, "Please select range correctly. Starting Page Number must be smaller\n than Ending Page NUmber.", "Not Valid Range !", 0);

}
}


    
    
    }

    public void printall(){
            while(i<=j)
            {
                jta.append(""+i);
            if(i!=j){
                jta.append(", ");    
                    }
                i++;
            }
            }
    
    public void printodd(){
    while(i<=j){
                if(i%2!=0){
                            jta.append(""+i);
                                            if(i!=j){
                                                    jta.append(", ");    
                            }}

                        i++;
                }
    
    }
    
    public void printeven(){
             while(i<=j){
                            if(i%2==0){
                                        jta.append(""+i);
                                                        if(i!=j){
                                                                jta.append(", ");    
                                        }}

                                i++;
                         } 
     }
    
    public void printrandom(){
         while(i<=j){
                    Random r = new Random();
                    int rand= r.nextInt(j-i) + i;;
                    System.out.println(rand);
                    jta.append(""+rand);
           if(i!=j){
                    jta.append(", ");    
                    }
                    i=rand;
                    i++;
                    }
    
    }
    
    public void printprime(){
    
    while(i<=j){
               boolean isPrime = true;
               //check to see if the number is prime
               for(int k=2; k < i ; k++){
               if(i % k == 0){
               isPrime = false;
               break;
               }
               }
               // print the number
               if(isPrime)
               {    
               jta.append(""+i);
               if(i!=j){
               jta.append(", ");    
               }    
               System.out.print(i + " \n");
               }
               i++;
       }
    
    }
    
    
    public void printallr(){
            while(j>=i)
            {
                jta.append(""+j);
            if(j!=i){
                jta.append(", ");    
                    }
                j--;
            }
            }
    
    public void printoddr(){
    while(j>=i){
                if(j%2!=0){
                            jta.append(""+j);
                                            if(j!=i){
                                                    jta.append(", ");    
                            }}

                        j--;
                }
    
    }
    
    public void printevenr(){
             while(j>=i){
                            if(j%2==0){
                                        jta.append(""+j);
                                                        if(j!=i){
                                                                jta.append(", ");    
                                        }}

                                j--;
                         } 
     }
    
    public void printrandomr(){
         while(j>=i){
                    Random r = new Random();
                    int rand= r.nextInt(j-i) + i;;
                    System.out.println(rand);
                    jta.append(""+rand);
           if(j!=i){
                    jta.append(", ");    
                    }
                    j=rand;
                    j--;
                    }
    
    }
    
    public void printprimer(){
    
    while(j>=i){
               boolean isPrime = true;
               //check to see if the number is prime
               for(int k=2; k < j ; k++){
               if(j % k == 0){
               isPrime = false;
               break;
               }
               }
               // print the number
               if(isPrime)
               {    
               jta.append(""+j);
               if(j!=i){
               jta.append(", ");    
               }    
               System.out.print(j + " \n");
               }
               j--;
       }
    
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
     try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } 
        catch (Exception err) {
            //System.out.println("Error loading Linux Theme:" + err.toString());
        }
    Pagenumbergenerator pg= new Pagenumbergenerator();

}
        // TODO code application logic here
    }

